package com.gua.encrypt.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.gua.common.exception.BusinessExecption;

import cn.hutool.crypto.asymmetric.RSA;

@ConditionalOnProperty(prefix = "crypto", name = "type", havingValue = "rsa", matchIfMissing = false)
@Configuration
public class RSACryptConfig {

	@Autowired
	private RSACryptProperties rsaCryptProperties;

	@Bean
	public RSA rsa() {
		String privateKey = rsaCryptProperties.getPrivateKey();
		String publicKey = rsaCryptProperties.getPublicKey();
		if(rsaCryptProperties.getAlgorithm()==null 
				|| StringUtils.isEmpty(privateKey) 
				|| StringUtils.isEmpty(publicKey)) {
			throw new BusinessExecption(String.format("加密类型%s配置为ras时，RSA配置不能为空：%s, %s, %s", 
					"crypto.type", 
					"crypto.ras.algorithm", 
					"crypto.ras.privateKey", 
					"crypto.ras.publicKey"));
		}
		return new RSA(rsaCryptProperties.getAlgorithm().getValue(), 
				privateKey,
				publicKey);
	}

}
